#include "fig_bullet_rocket.h"
#include "fig_sfx_manager.h"



FIGBulletRocket::FIGBulletRocket(float DamagePower, bool IsEnemyBullet) : 
									FIGBullet(0.8f,
												false,
												DamagePower,
												FIGBullet::BULLET_TYPE_PLAYER_ROCKET),
									m_pSFX(NULL),
									m_bToGoForward(false)
{
	if(!IsEnemyBullet)
		Load("rocket_0001");
	else
		Load("rocket_0001_enemy");

	m_Acceleration = 0.5f;
	m_MaxSpeed = 2.0f;
	m_Speed = 0.0f;
	m_Rotation = 0;
	

}
	


void		FIGBulletRocket::Clear()
{
	if(m_pSFX)
		m_pSFX->ToDie();
	m_pSFX = NULL;
	FIGBullet::Clear();
}

void		FIGBulletRocket::Render(const math::Vec2& Offset,float Scale)
{
	if(!m_IsAlive)
	{
		return;
	}
	
	FIGBullet::Render(Offset,Scale);
}
	
void		FIGBulletRocket::RenderDebug(const math::Vec2& Offset,float Scale)
{
	FIGBullet::RenderDebug(Offset,Scale);
}
	
void		FIGBulletRocket::Tick(float dt)
{
	FIGBullet::Tick(dt);

	if(m_Delay > 0)
		return;

	if(!m_IsAlive)
	{
		if(m_pSFX)
		{
			m_pSFX->ToDie();
			m_pSFX = NULL;
		}
		return;
	}

	m_Speed += (m_Acceleration * dt); 
	if(m_Speed > m_MaxSpeed)
		m_Speed = m_MaxSpeed;
	m_Movement.SetMoveSpeed(m_Speed);

	if(m_State == STATE_GOFORWARD)
	{		
		// nothing to do
	}
	else
	{
		math::Vec2 Dir;
		if(m_State == STATE_GOLEFT)
		{
			Dir = math::Vec2( -1.0f,0.0f);			
		}
		else
		{
			Dir = math::Vec2( 1.0f,0.0f);
		}

		const float SPEED = 0.2f;
		m_Pos += ( Dir * dt * SPEED);
		m_Movement.SetCurrent(m_Pos);

		if( (m_Pos-m_StartingPos).Magnitude() > 0.05f)
		{
			m_Speed = m_MaxSpeed * 0.5f;
			m_State = STATE_GOFORWARD;
			m_Movement.SetTarget(0);		// 0 == go up
		}
		

	}
	SFXTick(dt);
}

void		FIGBulletRocket::SFXTick(float dt)
{
	math::Vec2 Dir = math::Vec2(0,1.0f);
	Dir.Rotate(-m_Rotation);
	Dir *= 0.025f;

	if(m_pSFX)
		m_pSFX->FollowMe(m_Pos + Dir );
}

void		FIGBulletRocket::ReSpawn(const math::Vec2& Pos,FIGBullet::STARTPOS StartDir,const math::Vec2& TargetDir)
{
	// clean previous effect sfx
	if(m_pSFX)
	{
		m_pSFX->ToDie();
		m_pSFX = NULL;
	}
	FIGBullet::ReSpawn(Pos,StartDir,TargetDir);
	m_Speed = m_MaxSpeed * 0.5f;
	m_StartingPos = Pos;

	if(!m_bToGoForward)
	{
		if(StartDir == STARTPOS_LEFT)
			SetState(FIGBulletRocket::STATE_GOLEFT);
		else if(StartDir == STARTPOS_RIGHT)
			SetState(FIGBulletRocket::STATE_GORIGHT);
	}
	else
	{
		//m_bToGoForward = false;
		SetState(FIGBulletRocket::STATE_GOFORWARD);
	}

	// Spawning smoke trail
	m_pSFX = FIGSFXManager::Instance().Spawn(FIGSfx::EXPLODING_ROKETSMOKE, m_Pos,1.0f);

}


void		FIGBulletRocket::SetState(FIGBulletRocket::ROCKETSTATE State)
{
	
	m_State = State;
}
